/*--------------------------------------------------------------------------------------------------
Yatak Operation System Project Copyright 2012
Author : Ilya Churaev
Started : 5 Oct 2012		Updated : 25 Jan 2013
File Name : command_shell.c	Purpose : Administration command for shell
--------------------------------------------------------------------------------------------------*/
#include <yatak/yatak.h>
#include <yatak/kheap.h>
#include <libc/stdio.h>
#include <keyboard.h>
#include <screen.h>
#include <string.h>
#include <shell.h>
#include <sched.h>
#include <users.h>
#include <time.h>
#include <elf.h>

extern struct system_info current_system;
extern int GetNumCopy ();

extern void print_user_str ();

int command_line (char * param)
{
	if (exec_line (param) == 2) kprintf("Error: Command not found!!!\n");
	return 0;
}

void PrintASCIITable ()
{
	int i;
	for (i = 0; i < 256; i++)
	{
		kprintf ("%d - %c ",i,i);
		if (!i%5)
			kprintf ("\n");
	}
	kprintf ("\n");
}

void print_whoami ()
{
	kprintf ("%s\n",whoami ());
}
void print_date ()
{
	sysdate d;
	get_date (&d);
	dateprint (d);
	kprintf ("\n");
}

void print_time ()
{
	systime t;
	get_time (&t);
	timeprint (t);
	kprintf ("\n");
}
void mem_usage ()
{
	kprintf("%d kB\n",get_memory_usage()/1024);
}

void sys_info ()
{
	PrintSysInfo (&current_system);
}

void Help ()
{
	kprintf("This is help for shell terminale from YATAK.\n\
	ascii - print ascii codes\n\
	cat - concatenate files and print\n\
	cd - change directory\n\
	clear - clear terminal\n\
	date - print date on screen\n\
	decoder - decoder [file_name] decoder files\n\
	echo - echo [string] print string\n\
	exit - shut down system\n\
	floppy - detect floppy drivers\n\
	help - help for this terminal\n\
	kill - kill [pid] kill process\n\
	ls - show file list\n\
	memusage - show usage memory\n\
	ps - show processes\n\
	reboot - reboot system\n\
	shell - start new copy shell\n\
	shut_down - power off\n\
	sys_info - show system information\n\
	su - change user ID\n\
	tc - setting of terminal collors\n\
	test_task - execute test empty task\n\
	time - print time on screen\n\
	whoami - show current user name\n");
}

void TerminalColors ()
{
	char str [64];
	int bg, f;
	kprintf ("Please, input color of backgroung (black,blue, green,cyan,red,magenta,brown,lg,dg,lbl,lg,lc,lr,lm,lbr,white): ");
	in_string (str, 64);
	if (!strcmp (str,"black"))
		bg = __BLACK;
	else if (!strcmp (str,"blue"))
		bg = __BLUE;
	else if (!strcmp (str,"green"))
		bg = __GREEN;
	else if (!strcmp (str,"cyan"))
		bg = __CYAN;
	else if (!strcmp (str,"red"))
		bg = __RED;
	else if (!strcmp (str,"magenta"))
		bg = __MAGENTA;
	else if (!strcmp (str,"brown"))
		bg = __BROWN;
	else if (!strcmp (str,"lg"))
		bg = __LIGHT_GREY;
	else if (!strcmp (str,"dg"))
		bg = __DARK_GREY;
	else if (!strcmp (str,"lbl"))
		bg = __LIGHT_BLUE;
	else if (!strcmp (str,"lg"))
		bg = __LIGHT_GREEN;
	else if (!strcmp (str,"lc"))
		bg = __LIGHT_CYAN;
	else if (!strcmp (str,"lr"))
		bg = __LIGHT_RED;
	else if (!strcmp (str,"lm"))
		bg = __LIGHT_MAGENTA;
	else if (!strcmp (str,"lbr"))
		bg = __LIGHT_BROWN;
	else if (!strcmp (str,"white"))
		bg = __WHITE;
	else
	{
		kprintf ("Unknown color!!!");
		return;
	}
	kprintf ("Please, input color of font (black,blue, green,cyan,red,magenta,brown,lg,dg,lbl,lg,lc,lr,lm,lbr,white): ");
	in_string (str, 64);
	if (!strcmp (str,"black"))
		f = __BLACK;
	else if (!strcmp (str,"blue"))
		f = __BLUE;
	else if (!strcmp (str,"green"))
		f = __GREEN;
	else if (!strcmp (str,"cyan"))
		f = __CYAN;
	else if (!strcmp (str,"red"))
		f = __RED;
	else if (!strcmp (str,"magenta"))
		f = __MAGENTA;
	else if (!strcmp (str,"brown"))
		f = __BROWN;
	else if (!strcmp (str,"lg"))
		f = __LIGHT_GREY;
	else if (!strcmp (str,"dg"))
		f = __DARK_GREY;
	else if (!strcmp (str,"lbl"))
		f = __LIGHT_BLUE;
	else if (!strcmp (str,"lg"))
		f = __LIGHT_GREEN;
	else if (!strcmp (str,"lc"))
		f = __LIGHT_CYAN;
	else if (!strcmp (str,"lr"))
		f = __LIGHT_RED;
	else if (!strcmp (str,"lm"))
		f = __LIGHT_MAGENTA;
	else if (!strcmp (str,"lbr"))
		f = __LIGHT_BROWN;
	else if (!strcmp (str,"white"))
		f = __WHITE;
	else
	{
		kprintf ("Unknown color!!!");
		return;
	}
	settextcolor(f, bg);
	cls ();
}

int executive_line (char *line)
{
	char buf [256];
	strsep (&line,".");
	strsep (&line,"/");
	
	fs_node_t *fsnode = finddir_fs(GetCurrentNodeDir(), line);
	if (!fsnode)
	{
		kprintf ("Cannot open file: %s\n", line);
		return 1;
	}
	
	int i = fread_line(fsnode, 0, 256, buf);
	dprintf ("[executive_line] buf = %s num = %d\n",buf, i);
	if (!strcmp (buf,"!./yatak/interp"))
	{
		return shell_interp (fsnode);
	}
	else
	{
		static struct dirent *node = 0;
		int j = 0;
		while ((node = readdir_fs(fs_root, j)) != 0)
		{
			dprintf ("[executive_line] Cycle: inode = %d, node->name = %s, fsnode->name = %s\n", j, node->name, fsnode->name);
			if (!strcmp (node->name,fsnode->name))
				break;
			j++;
		}
		dprintf ("[executive_line] inode = %d, node->name = %s, fsnode->name = %s\n", j, node->name, fsnode->name);
		if (strcmp (node->name,fsnode->name))
		{
			dprintf ("[executive_line] Error!!! node->name != fsnode->name\n");
			return 3;
		}
		int seek = 0;
		do
		{
			i = read_fs(fsnode, seek, 256, buf);
			seek += i;
		}
		while (i);
		if (!load_elf(j, seek))
			return 0;
	}
	kprintf ("%s has unknown type\n", fsnode->name);
	return 2;
}
